package jay.home.bet.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jay.home.bet.domain.Item;
import jay.home.bet.domain.Order;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ListOrdersAction extends BaseAction
{
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
	{
		String username = request.getParameter("username");
		String today = request.getParameter("today");
		String compid = request.getParameter("compid");
		String eventid = request.getParameter("eventid");
		String oddtype = request.getParameter("oddType");
		int page = getPageNumber(request);
		int range = 50;
		
		List<Order> orders = null;
		
		if(today != null)
			if(username != null)orders = getOrderService().getTodayOrdersByUser(username, page, range);
			else orders = getOrderService().getTodayOrders(page, range);
		if(compid != null)
			orders = getOrderService().getOrdersByComp(compid, page, range);
		if(eventid != null && oddtype != null)
			orders = getOrderService().getOrdersByEventAndOdd(eventid, oddtype, page, range);
		for(Order order : orders)
			for(Item item : order.getItems())
				item.setEvent(getOddsService().getEvent(item.getEventid()));
		request.setAttribute("orders", orders);
		return mapping.findForward("SUCCESS");
	}
}
